package com.zking.frame;

import com.zking.dao.FoodDao;
import com.zking.entity.Food;
import com.zking.utils.Tool工具;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Wares extends JDialog {

    public Wares(JFrame owner, boolean modal, String title, Map<String,Integer> map,String sort,String shop){
        super(owner,modal);

        // 存储商铺商品
        List<Food> fonts = new ArrayList<Food>();
        JLabel label;

        // 获取点击商品的价格

        FoodDao foodDao = new FoodDao();
        // 获取商店菜品
        fonts = foodDao.queryFoodName(sort,shop);

        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setBounds(owner.getX()+15, owner.getY()+400, 600, 500);
        this.setResizable(false);// 设置窗口不可调整大小
        this.setTitle("以点商品");
        this.setLayout(null);

        //设置标题
        JLabel jLabel = new JLabel("你现在已点的商品",0);
        jLabel.setBounds(0,10,600,50);
        jLabel.setForeground(new Color(98,5,111));
        jLabel.setFont(new Font("黑体",Font.BOLD,20));
        this.add(jLabel);

        JPanel jPanel = new JPanel();
        jPanel.setBounds(0,0,560,1000);
        jPanel.setPreferredSize(new Dimension(545,1000));
        jPanel.setLayout(null);
        jPanel.setBackground(Color.white);
        //获取面板
        jPanel.add(getJpanel(map,fonts,sort,shop));

        //设置显示的滚动框
        JScrollPane jScrollPane = new JScrollPane(jPanel);
        jScrollPane.setBorder(new MatteBorder(2, 2, 2, 2, Color.green));
        jScrollPane.setBounds(10,65,570,390);
        this.add(jScrollPane);
        setVisible(true);
    }

    public JPanel getJpanel(Map<String,Integer> map,List<Food> fonts,String sort,String shop) {
        JPanel jPanel = new JPanel();
        jPanel.setBounds(0, 0, 560, 1000);
        jPanel.setPreferredSize(new Dimension(545, 1000));
        jPanel.setLayout(null);
        jPanel.setBackground(Color.white);

        int ab = 0;
        for (String key : map.keySet()) {
            JLabel jLabel1 = new JLabel(key);
            jLabel1.setBounds(0, 95 * ab, 400, 40);
            jLabel1.setFont(new Font("黑体", Font.BOLD, 30));
            jPanel.add(jLabel1);

            boolean abc = true;
            for (int bc = 0; bc < fonts.size(); bc++) {
                if (fonts.get(bc).getName().equals(key) && abc) {
                    JLabel jLabel2 = new JLabel("单价：￥" + fonts.get(bc).getPrice());
                    jLabel2.setForeground(Color.RED);
                    jLabel2.setBounds(0, 95 * ab + 50, 300, 40);
                    jLabel2.setFont(new Font("黑体", Font.BOLD, 30));
                    jPanel.add(jLabel2);
                    abc = false;
                }
            }
            abc = true;

            ImageIcon imageIcon = new ImageIcon("picture/工具/减.jpg");
            JLabel jLabel3 = new JLabel(imageIcon);
            jLabel3.setBounds(400, 95 * ab + 25, 40, 40);
            jPanel.add(jLabel3);

            JLabel label = new JLabel(map.get(key) + " ", 0);
            label.setBounds(445, 95 * ab + 25, 50, 40);
            label.setFont(new Font("黑体", Font.BOLD, 30));
            jPanel.add(label);

            JLabel finalLabel = label;
            jLabel3.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 1) {
                        if (map.get(key) > 0) {
                            map.put(key, map.get(key) - 1);
                            finalLabel.setText(map.get(key) + "");
                            Tool工具.setJLabel4(sort, shop);
                        }
                        if (map.get(key) == 0) {
                            map.remove(key);
                            jPanel.removeAll();
                            jPanel.add(getJpanel(map,fonts,sort,shop));
                            jPanel.repaint();
                            jPanel.revalidate();
                        }
                        if (map.keySet().size()==0){
                            dispose();
                        }
                    }
                }
            });

            ImageIcon imageIcon1 = new ImageIcon("picture/工具/加.jpg");
            JLabel jLabel4 = new JLabel(imageIcon1);
            jLabel4.setBounds(490, 95 * ab + 25, 50, 40);
            jPanel.add(jLabel4);
            jLabel4.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 1) {
                        map.put(key, map.get(key) + 1);
                        finalLabel.setText(map.get(key) + "");
                        Tool工具.setJLabel4(sort, shop);
                    }
                }
            });
            ab++;
        }
        return jPanel;
    }
}
